#include "http.hpp"
#define WWWROOT "./wwwroot"
#define TIMEOUT 10

void SplitTest()
{
    string t1 = "1,2,3,34";
    string t2 = "123,,,1";
    string t3 = "123,,,";

    vector<string> vts = {t1,t2,t3};
    vector<vector<string>> vvs(3);
    for(int i = 0; i < vvs.size(); i++)
    {
        Util::Split(vts[i], ",", &vvs[i]);
    }

    for(auto& vs: vvs)
    {
        cout << "size: " << vs.size() << " ";
        for(auto& s : vs)
        {
            cout << s << " ";
        }
        cout << endl;
    } 
}

void RWFileTest()
{
    Buffer buffer;
    Util::ReadFile("./http.hpp", &buffer);
    cout << buffer.GetReadPosition();

    Util::WriteFile("new.txt", &buffer);
}

void UrlEDTest()
{
    string res = Util::UrlEncode("c++?/123",false);
    cout << res << endl;

    string dres = Util::UrlDecode(res,false);
    cout << dres << endl;
}

void MsgHashTest()
{
    string t1 = "new.txt";
    string t2 = "new.jpgl";
    int ts1 = 200;
    int ts2 = 600;

    cout << Util::StatusDesc(ts1) << endl;
    cout << Util::StatusDesc(ts2) << endl;
    cout << Util::ExtractMime(t1) << endl;
    cout << Util::ExtractMime(t2) << endl;
}

void FileJudgeTest()
{
    cout << Util::IsDirectory("../http") << endl;
    cout << Util::IsDirectory("./http.hpp") << endl;
    cout << Util::IsRegular("../http") << endl;
    cout << Util::IsRegular("./http.hpp") << endl;
}

void ValidPathTest()
{
    cout << Util::IsValidPath("/abc/index.html/../../") << endl;
    cout << Util::IsValidPath("../abc/index.html") << endl;
    cout << Util::IsValidPath("/abc/../index.html") << endl;
}

void RegexTest()
{
    string line = "GET /bitejiuyeke?a=b&c=d HTTP/1.1\r\n";
    std::smatch sm;
    std::regex e ("(GET|HEAD|POST|PUT|DELETE) ([^?]+)(?:\\?(.*?))? (HTTP/1\\.[0|1])(?:\n|\r\n)?", std::regex::icase);
    //std::regex e("(GET|HEAD|POST|PUT|DELETE) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?", std::regex::icase);
    std::regex_match(line, sm, e);
    for(int i = 0; i < sm.size(); i++)
    {
        cout << sm[i] << endl;
    }
    cout << "---------------------------" << endl;
}


string ReqStr(const HttpRequest& req, HttpResponse* rsp)
{
    string req_info = "";
    req_info += req._method +" " + req._path + " " + req._version + "\r\n";
    for (auto &it : req._params) {
        req_info += it.first + ": " + it.second + "\r\n";
    }

    for(auto headers : req._headers)
    {
        req_info += headers.first + ": " + headers.second + "\r\n";
    }

    req_info += "\r\n";
    req_info += req._body;

    return req_info;
}

void GetHandlerTest(HttpRequest& req, HttpResponse* rsp)
{
    rsp->SetContent(ReqStr(req, rsp), "text/plain");
    //sleep(12);
}
void PostHandlerTest(HttpRequest& req, HttpResponse* rsp)       //post方法提交的内容放在body中
{
    rsp->SetContent(ReqStr(req, rsp), "text/plain");
}
void PutHandlerTest(HttpRequest& req, HttpResponse* rsp)
{
    string info = ReqStr(req, rsp);
    Util::WriteFile(WWWROOT + req._path, &req._body);
    //rsp->SetContent(req._body, "text/plain");
}
void DeleteHandlerTest(HttpRequest& req, HttpResponse* rsp)
{
    rsp->SetContent(ReqStr(req, rsp), "text/plain");
}

int main()
{
    HttpServer server(8088, TIMEOUT);
    server.SetBaseDir(WWWROOT);
    server.SetThreadCount(3);
    server.SetGetHandle("/hello", GetHandlerTest);
    server.SetPostHandle("/login", PostHandlerTest);
    server.SetPutHandle("/123.txt", PutHandlerTest);
    server.SetDeleteHandle("/123.txt", DeleteHandlerTest);
    server.Start();
    return 0;
}